package com.lw.service.impl;

import com.lw.entity.TbDictData;
import com.lw.entity.table.TbDictDataTableDef;
import com.lw.service.TbDictDataService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.lw.entity.TbDictType;
import com.lw.mapper.TbDictTypeMapper;
import com.lw.service.TbDictTypeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static cn.dev33.satoken.SaManager.log;

/**
 * 字典类型 服务层实现。
 *
 * @author lw
 * @since 2025年05月06日 19:01:57
 */
@Service
public class TbDictTypeServiceImpl extends ServiceImpl<TbDictTypeMapper, TbDictType> implements TbDictTypeService {
    @Resource
    TbDictDataService tbDictDataService;

    @Override
    public List<TbDictType> getAllList() {
        List<TbDictType> typeList = list();
        List<TbDictData> dataList = tbDictDataService.list();
        for (TbDictType type : typeList) {
            for (TbDictData data : dataList) {
                if (type.getId().equals(data.getDictTypeId())) {
                    type.getDataList().add(data);
                }
            }
        }
        return typeList;
        
    }

    @Override
    public Boolean saveDictTypeAndDictData(TbDictType tbDictType) {
        // 保存字典类型
        tbDictType.setUpdateDate(LocalDateTime.now());
        tbDictType.setCreateDate(LocalDateTime.now());
        boolean save = save(tbDictType);
        log.info("保存字典类型：{}", save);
        List<TbDictData> dataList = tbDictType.getDataList();
        if(dataList.isEmpty()){
            return true;
        }
        // 保存字典数据
        for (TbDictData data : dataList) {
            data.setDictTypeId(tbDictType.getId());
            data.setCreateDate(LocalDateTime.now());
            data.setUpdateDate(LocalDateTime.now());
        }
        boolean saveBatch = tbDictDataService.saveBatch(dataList);
        log.info("保存字典数据：{}", saveBatch);
        return save && saveBatch;
    }

    @Override
    public Boolean updateDictTypeAndDictData(TbDictType tbDictType) {
        // 更新字典类型
        tbDictType.setUpdateDate(LocalDateTime.now());
        boolean update = updateById(tbDictType);
        log.info("更新字典类型：{}", update);
        List<TbDictData> dataList = tbDictType.getDataList();
        if(dataList.isEmpty()){
            return true;
        }
        // 更新字典数据
        for (TbDictData data : dataList) {
            data.setId(null);
            data.setDictTypeId(tbDictType.getId());
            data.setCreateDate(LocalDateTime.now());
            data.setUpdateDate(LocalDateTime.now());
        }
        // 删除原有的字典数据
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(TbDictDataTableDef.TB_DICT_DATA.DICT_TYPE_ID.getName(), tbDictType.getId());
        boolean remove = tbDictDataService.remove(queryWrapper);
        log.info("删除原有的字典数据：{}", remove);
        // 保存新的字典数据
        boolean saveBatch = tbDictDataService.saveBatch(dataList);
        log.info("保存新的字典数据：{}", saveBatch);
        return update && remove && saveBatch;
    }
}
